<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="com.boxin.ims.modules.wechat.entity.Lottery"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>抽奖管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
	
	
		var lotteryId;
	
		$(document).ready(function() {
			$("#name").focus();
			lotteryId = $('#lottery_id').val();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
			var alldata = "${alldata}";//"25175,Dicky.25175.com/BBS1,Dicky.25175.Com,AppleBBS,Dicky,大雨点,51windows,blueidea,csdn,google,baidu,msn,qq,cctv,microsoft,adobe"    //抽奖的姓名，以,为分隔

			var alldataarr = alldata.split(",");
			var num = alldataarr.length-1;
			var timer ;
			
			function change(){
				var myset = new Array();
				var n =0;
				while(true){
					n++;
					var rval = alldataarr[GetRnd(0,num)];
					if(n >= 100) break;
					if($.inArray(rval,myset)>-1){	//如果存在则不添加
						continue;
					}else{
						myset.push(rval);
					}
					
					if(myset.length > '${lottery.peramount}') break;
					
				}
				
				$.each(myset,function(key,value){
					var mobile = value;
					value = value.replace(value.substring(3,7),'****');
					$('#lettery_number'+(key+1)).html(value);
					$('#lettery_number'+(key+1)).attr("mobile",mobile);
				}) ;
				
			}

			
			
			
			function setLotteryId(value){
				lotteryId = value;
				$('lottery_id').attr('disabled','disabled');
			}
			
			
			function start(){
				if(alldata.length<11){
					alert("没有找到新微信新会员的手机号码，请刷新该页面后重试！");
					return ;
				}
				
				if(lotteryId == null){
			    	alert("未设置奖品，无法开始！");
			    	return;
			    }
				
			    $('#lottery_title').html("抽奖进行中...");
				$('#btnStart').attr("disabled","disabled");
				$('#btnStop').removeAttr("disabled");
			    clearInterval(timer);
			    timer = setInterval('change()',50);    //50（毫秒）为变换间隔，越小变换的越快
			}

			function stop1(){
			    clearInterval(timer);
			    var nums = $('.lettery_number');
			    $.each(nums,function(key,value){
			    	var id = '#lettery_number'+(key+1);
			    	var mobile = $(this).attr('mobile');
					var html = $('#lettery_number'+(key+1)).html();
					$('#lettery_number'+(key+1)).html(html+"  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id='m_operation"+(mobile)+"' class='btn btn-danger'   onclick='drop(this)'>作废</button>");
				}) ;
			    
			}
			
			
			function stop(){
			    clearInterval(timer);
			    
				$('#btnStop').attr("disabled","disabled");
			    var val = $('#lettery_number').val();
			    $('#lottery_title').html("中奖号码为:");
			    var lotterys = $('.lettery_number');
			    var mobiles = "";
			    $.each(lotterys,function(key,value){
			    	v = $(value).attr('mobile');
			    	mobiles =  mobiles + v + ",";
			    });
			    
			    if(lotteryId == null){
			    	alert("未设置奖品，无法开始！");
			    }
			    
			    var params = {'mobile':mobiles,"id":'${lottery.id}'};
			    $.post('${ctx}/wechat/lottery/dolottery',params,function(data){
			    	var nums = $('.lettery_number');
				    $.each(nums,function(key,value){
				    	var mobile = $(this).attr('mobile');
						var html = $('#lettery_number'+(key+1)).html();
						$('#lettery_number'+(key+1)).html(html);//+"  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button id='m_operation"+(mobile)+"' class='btn btn-danger'   onclick='drop(this)'>作废</button>");
					}) ;
				},'json');
			}

			function GetRnd(min,max){
			    return parseInt(Math.random()*(max-min+1));
			}
			
			/**
			  作废
			***/
			
			function drop(value){
				var spanid = value.id;
				var mobile = spanid.replace('m_operation','');
				
				
				var params = {'mobile':mobile,"id":lotteryId};
			    $.post('${ctx}/wechat/lottery/droplottery',params,function(data){
				     
				},'json');
				
			}
			
			
			
		
		
	</script>
</head>
<body>
	<div class="container" align="center">
		
		<c:if test="${lotteryFlag eq '0' }">
			<br/>
			<br/>
			<br/>
			<br/>
			
			<h2 id="lottery_title" style="color:red">现在参与抽奖的人数为${persons }人,因不足${lottery.peramount }人暂时不能进行抽奖活动.</h2>
		</c:if>
		<c:if test="${lotteryFlag eq '1' }">
		<h4 id="lottery_title" style="color:red">本次奖品: <span id="lottery_id" value="${lottery.id }">${lottery.name },现在参与抽奖的人数为${persons }人.</span>
		 
		 </h4>
		 
		<shiro:hasPermission name="wechat:lottery:view">
			<% 
				Lottery lottery = (Lottery)request.getAttribute("lottery");
				List<Map<String,Object>> result = (List<Map<String,Object>>)request.getAttribute("mobileList");
				int loop = 10;
				if(lottery != null){
					loop = lottery.getPeramount();
					
				}
				for(int i=1;i<loop+1;i++){
					String mobile = result.get(i-1).get("mobile").toString();
					mobile = mobile.replace(mobile.substring(3,7), "****");				
			%>
			<%= "<h3>	<span id=\"lettery_number"+i+"\" name=\"lettery_number"+i+"\" class=\"lettery_number\" >"+mobile+"</span> </h3>" %>
			<%} %>
			<br>
			<button id="btnStart" onClick="start()" accesskey="s" class="btn btn-danger">
				开始(<U>S</U>)
			</button>
			<button id="btnStop" onClick="stop()" accesskey="o" class="btn btn-success" disabled="disabled">
				停止(<U>O</U>)
			</button>
		</shiro:hasPermission>
		<br>
		<br>
		<br>
		</c:if>
	</div>
</body>
</html>
